﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Vehicle_Maintenance
{
    public partial class Change_User_or_Car : Form
    {
        private bool changed = false;

        public Change_User_or_Car()
        {
            InitializeComponent();
        }

        private void Change_User_or_Car_Load( object sender, EventArgs e )
        {
            // TODO: This line of code loads data into the 'auto.Vehicles' table. You can move, or remove it, as needed.
            this.vehiclesTableAdapter.Fill( this.auto.Vehicles );
            // TODO: This line of code loads data into the 'auto.Users' table. You can move, or remove it, as needed.
            this.usersTableAdapter.Fill( this.auto.Users );
            DataTable UsersCars = this.vehiclesTableAdapter.GetDataByOwner(Convert.ToInt32(cb_User.SelectedValue.ToString()));

            cb_Car.DataSource = UsersCars;
            cb_Car.DisplayMember = "Name";
            cb_Car.ValueMember = "VIN";
            
        }

        private void btn_AddUser_Click( object sender, EventArgs e )
        {
            User dlg = new User();
            if ( dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK )
            {
                // strip user infor from dialog
                changed = true;
                Person p       = new Person();
                p.FirstName    = ( (TextBox)dlg.Controls.Find( "tb_FName", true )[ 0 ] ).Text;
                p.LastName     = ( (TextBox)dlg.Controls.Find( "tb_LName", true )[ 0 ] ).Text;
                string txt     = ( (ComboBox)dlg.Controls.Find( "cb_State", true )[ 0 ] ).Text;
                p.LicenseState = txt.Substring( txt.Length - 2 );
                p.LicenseNo    = ( (TextBox)dlg.Controls.Find( "tb_LicNum", true )[ 0 ] ).Text;
                p.Insurrance   = ( (TextBox)dlg.Controls.Find( "tb_Insur", true )[ 0 ] ).Text;
                p.New();
                usersTableAdapter.Fill( auto.Users );
                cb_User.SelectedIndex = cb_User.Items.Count - 1;
                //usersBindingSource.ResetBindings(true);
            }
        }

        private void btn_AddCar_Click( object sender, EventArgs e )
        {
            DataTable UsersCars = this.vehiclesTableAdapter.GetDataByOwner(Convert.ToInt32(cb_User.SelectedValue.ToString()));
            Car dlg = new Car();
            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Vehicle v = new Vehicle();
                v.VIN = ((TextBox)dlg.Controls.Find("tb_VIN", true)[0]).Text;
                v.Owner_ID = cb_User.SelectedValue.ToString();
                v.Year = ((NumericUpDown)dlg.Controls.Find("nud_Year", true)[0]).Value.ToString();
                v.Make = ((TextBox)dlg.Controls.Find("tb_Make", true)[0]).Text;
                v.Model = ((TextBox)dlg.Controls.Find("tb_Model", true)[0]).Text;
                v.Varient = ((TextBox)dlg.Controls.Find("tb_Varient", true)[0]).Text;
                v.T_Gal = "0";
                v.T_Spent = "0";
                v.Orig_Odom = ((TextBox)dlg.Controls.Find("tb_COdom", true)[0]).Text;
                v.Curr_Odom = ((TextBox)dlg.Controls.Find("tb_COdom", true)[0]).Text;
                v.Name = ((TextBox)dlg.Controls.Find("tb_Name", true)[0]).Text;
                v.Reg_Num = ((TextBox)dlg.Controls.Find("tb_RegNum", true)[0]).Text;
                v.Tire_Size = ((TextBox)dlg.Controls.Find("tb_TSize", true)[0]).Text;
                v.Eng_Size = ((TextBox)dlg.Controls.Find("tb_ESize", true)[0]).Text;
                v.Insur = ((TextBox)dlg.Controls.Find("tb_Insur", true)[0]).Text;
                v.New();
                UsersCars = this.vehiclesTableAdapter.GetDataByOwner(Convert.ToInt32(cb_User.SelectedValue.ToString()));
                cb_Car.DataSource = UsersCars;
                cb_Car.DisplayMember = "Name";
                cb_Car.ValueMember = "VIN";

            }
            UsersCars = this.vehiclesTableAdapter.GetDataByOwner(Convert.ToInt32(cb_User.SelectedValue.ToString()));

            cb_Car.DataSource = UsersCars;
            cb_Car.DisplayMember = "Name";
            cb_Car.ValueMember = "VIN";
        }

        private void cb_User_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (changed)
            {
                changed = false;
            }
            else
            {
                DataTable UsersCars = this.vehiclesTableAdapter.GetDataByOwner(Convert.ToInt32(cb_User.SelectedValue.ToString()));
                if (UsersCars.Rows.Count != 0)
                {
                    cb_Car.DataSource = UsersCars;
                    cb_Car.DisplayMember = "Name";
                    cb_Car.ValueMember = "VIN";
                }
                else
                {
                    Car dlg = new Car();
                    if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        Vehicle v = new Vehicle();
                        v.VIN = ((TextBox)dlg.Controls.Find("tb_VIN", true)[0]).Text;
                        v.Owner_ID = cb_User.SelectedValue.ToString();
                        v.Year = ((NumericUpDown)dlg.Controls.Find("nud_Year", true)[0]).Value.ToString();
                        v.Make = ((TextBox)dlg.Controls.Find("tb_Make", true)[0]).Text;
                        v.Model = ((TextBox)dlg.Controls.Find("tb_Model", true)[0]).Text;
                        v.Varient = ((TextBox)dlg.Controls.Find("tb_Varient", true)[0]).Text;
                        v.T_Gal = "0";
                        v.T_Spent = "0";
                        v.Orig_Odom = ((TextBox)dlg.Controls.Find("tb_COdom", true)[0]).Text;
                        v.Curr_Odom = ((TextBox)dlg.Controls.Find("tb_COdom", true)[0]).Text;
                        v.Name = ((TextBox)dlg.Controls.Find("tb_Name", true)[0]).Text;
                        v.Reg_Num = ((TextBox)dlg.Controls.Find("tb_RegNum", true)[0]).Text;
                        v.Tire_Size = ((TextBox)dlg.Controls.Find("tb_TSize", true)[0]).Text;
                        v.Eng_Size = ((TextBox)dlg.Controls.Find("tb_ESize", true)[0]).Text;
                        v.Insur = ((TextBox)dlg.Controls.Find("tb_Insur", true)[0]).Text;
                        v.New();
                        UsersCars = this.vehiclesTableAdapter.GetDataByOwner(Convert.ToInt32(cb_User.SelectedValue.ToString()));
                        cb_Car.DataSource = UsersCars;
                        cb_Car.DisplayMember = "Name";
                        cb_Car.ValueMember = "VIN";
                        cb_Car.SelectedIndex = cb_Car.Items.Count - 1;
                    }
                    else
                    {
                        cb_Car.DataSource = null;
                        cb_Car.Items.Clear();
                    }
                }
            }
        }
    }
}
